package com.stage.crud;

import com.stage.helper.DataBaseManager;
import com.stage.helper.InputManager;
import com.stage.helper.ObjectManager;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by Stage on 2017/3/24.
 */
public class ObjectCrud {
    private ObjectManager objectManager = new ObjectManager();
    private InputManager inputManager = new InputManager();

    public static void main(String[] args) {
        ObjectCrud objectCrud = new ObjectCrud();
        objectCrud.crud();
    }

    public void crud() {
        ObjectCrud objectCrud = new ObjectCrud();
        DataBaseManager dataBaseManager = new DataBaseManager();
        Connection connection = dataBaseManager.connectDataBase("JavaHomeWork");
        try {
            while (true) {
                printConsole();
                int select = inputManager.inputInteger("请输入选项");
                switch (select) {
                    case 1:
                        printObjects(dataBaseManager.queryDataFromTable(connection, "Person", "com.stage.annotation.Person"));
                        break;
                    case 2:
                        if (dataBaseManager.addData(connection, "Person", objectCrud.addObjectByLabel("com.stage.annotation.Person"))) {
                            System.out.println("添加成功");
                        } else {
                            System.out.println("添加失败");
                        }
                        break;
                    case 3:
                        int id = inputManager.inputInteger("请输入人员编号");
                        if (dataBaseManager.deleteDataById(connection, "Person", id)) {
                            System.out.println("删除成功");
                        } else {
                            System.out.println("删除失败");
                        }
                        break;
                    case 4:
                        int updateId = inputManager.inputInteger("请输入人员编号");
                        if (dataBaseManager.updateDataById(connection, "Person", updateId, objectCrud.addObjectByLabel("com.stage.annotation.Person"))) {
                            System.out.println("修改成功");
                        } else {
                            System.out.println("修改失败");
                        }
                        break;
                }
                if (select == 5) {
                    break;
                }
            }
        } finally {
            try {
                connection.close();
            } catch (SQLException e) {
                System.out.println("系统故障");
            }
        }
    }
    public Object addObjectByLabel(String className) {
        return objectManager.getUserInputByLabel(className);
    }

    public void printObjects(HashMap<Object, String> objects) {
        Iterator<Map.Entry<Object, String>> entryKeyIterator = objects.entrySet().iterator();
        while (entryKeyIterator.hasNext()) {
            Map.Entry<Object, String> e = entryKeyIterator.next();
            System.out.println("*********************");
            System.out.println("编号为：" + e.getValue());
            objectManager.showObjectByLabel(e.getKey());
            System.out.println("*********************");
        }
    }

    public void printConsole() {
        System.out.println("----------------------");
        System.out.println("输入1：查看人员信息");
        System.out.println("输入2：添加人员");
        System.out.println("输入3：删除人员");
        System.out.println("输入4：修改人员信息");
        System.out.println("输入5：退出");
        System.out.println("----------------------");
    }

}
